const api = require('../../utils/interface.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idx:null,
    ridx:null,
    alertData:[],
    list: [],
    alertState:false,
    name:"",
    src:"",
    dto:null,
    opState:""
  },
  commercial(e){
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    if(index == 1){
      this.setData({
        alertState:true
      })
    }else{
      this.setData({ ridx: -1, src:''})
    }
    this.setData({
      idx:index,
    })
    
  },
  selectRadio(e){
    let index = e.currentTarget.dataset.idx
    this.setData({ ridx:index})
    console.log(e)
  },
  confirm(){
    this.setData({
      name: this.data.alertData[this.data.ridx].name,
      alertState:false,
      src: this.data.alertData[this.data.ridx].imgUrl
    })
    console.log(this.data.name)
  },
  go_next(){
    console.log(this.data.idx)
    console.log(this.data.ridx)
    if (!this.data.idx && this.data.idx!=0){
      wx.showModal({
        title: '提示',
        content: '请选择认证车的类型',
        showCancel:false
      })
      return false
    }
    let user = wx.getStorageSync("menberInfo")
    let options = {
      certificationType: this.data.idx,
      memberId: user.id,
      useType: this.data.idx==4?1:0,
      sourceType: this.data.idx==2?this.data.ridx:"",
      picPositiveUrl:"",
      picBackUrl:"",
      picPlatformUrl:"",
      carNumber:"",
      index: Number(this.data.idx)+1,
      ridx: this.data.ridx,
      name: this.data.name
      }
      console.log(this.data.idx)
    if (this.data.idx == 0 || this.data.idx == 2 || this.data.idx == 3){
      if (this.data.dto && this.data.dto.certificationType - 1 != 1 && this.data.opState!=2){
        options.picBackUrl = this.data.dto.picBackUrl
        options.carNumber = this.data.dto.carNumber
        options.picPositiveUrl = this.data.dto.picPositiveUrl
        wx.navigateTo({
          url: './cardUp/index?params=' + JSON.stringify(options),
        })
      }else{
        wx.navigateTo({
          url: './cardUp/index?params=' + JSON.stringify(options),
        })
      }
     
    } else if (this.data.idx == 1 && this.data.ridx>=0 ){
      if (this.data.dto && this.data.dto.certificationType - 1 == 1 && this.data.dto.sourceType == this.data.ridx && this.data.opState != 2){
        options.picBackUrl = this.data.dto.picBackUrl
        options.carNumber = this.data.dto.carNumber
        options.picPositiveUrl = this.data.dto.picPositiveUrl
        wx.navigateTo({
          url: './nwCartUp/index?params=' + JSON.stringify(options),
        })
      }else{
        wx.navigateTo({
          url: './nwCartUp/index?params=' + JSON.stringify(options),
        })
      }
      
    }else{
      wx.showModal({
        title: '提示',
        content: '请选择认证车的类型',
        showCancel: false
      })
    }
  },
  onLoad(options){
    console.log(options)
    this.getList()
    this.getList2()
    if (options.carInfo) {
      let dto = JSON.parse(options.carInfo)
      console.log(dto)
      this.setData({
        idx: dto.certificationType - 1,
        ridx: dto.certificationType - 1 == 1 ? dto.sourceType : null,
        dto:dto,
        opState:options.state
      })
    }
  },
  getList(){
    api.memberCarCertificationType({}).then(res=>{
      console.log(res.data,'网路应用')
      this.setData({
        list:res.data.result
      })
    })
  },
  getList2(){
    api.memberCarSourceType({}).then(res => {
      console.log(res.data, '网路应用')
      res.data.result.map(item=>{
        item.src = '../../radio.png'
      })
      if (this.data.dto && this.data.dto.certificationType - 1==1){
        this.setData({
          src: res.data.result[this.data.dto.sourceType].imgUrl
        })
      }
      this.setData({
        alertData: res.data.result
      })
    })
  },
  //点击错号时触发的状态
  unState(){
    this.setData({
      alertState:false,
      ridx:null,
      src:""
    })
  }
})